package com.food.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.food.mapper.CommentsMapper;
import com.food.mapper.FoodsMapper;
import com.food.mapper.UsersMapper;
import com.food.pojo.Comments;
import com.food.pojo.Foods;
import com.food.pojo.Users;
import com.food.service.CommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements CommentsService {


    @Autowired
    CommentsMapper commentsMapper;

    @Autowired
    UsersMapper usersMapper;

    @Autowired
    FoodsMapper foodsMapper;

    @Override
    public List<Comments>  selectFoodsId(Integer foodsId){
        QueryWrapper<Comments> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("food_id", foodsId)
                .orderByDesc("comment_id");
        List<Comments> comments = commentsMapper.selectList(queryWrapper);
        for (Comments comment : comments) {
            String userId = comment.getUserId();
            QueryWrapper<Users> usersMapperQueryWrapper = new QueryWrapper<>();
            usersMapperQueryWrapper.eq("user_id", userId);
            Users users = usersMapper.selectOne(usersMapperQueryWrapper);
            comment.setUserAvatar(users.getUserAvatar());
        }
        return comments;
    }

    @Override
    public List<Comments> selectLimit3() {
        QueryWrapper<Comments> queryWrapper = new QueryWrapper<>();
        queryWrapper.last("limit 3");
        List<Comments> comments = commentsMapper.selectList(queryWrapper);
        return comments;
    }

    @Override
    public List<Comments> selectLimitAll(Integer userId) {
        // 通过userId获取用户的头像
        QueryWrapper<Users> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_id", userId);
        Users users = usersMapper.selectOne(userQueryWrapper);
        String userAvatar;
        if (users == null) {
            userAvatar = null;
        } else {
            userAvatar = users.getUserAvatar();
        }

        // 获取所有的评论
        QueryWrapper<Comments> commentsQueryWrapper = new QueryWrapper<>();
        commentsQueryWrapper.eq("user_id", userId);
        commentsQueryWrapper.orderByDesc("comment_id");

        List<Comments> comments = commentsMapper.selectList(commentsQueryWrapper);
        for (Comments comment : comments) {
            // 通过 foodId 获取美食对象
            String foodId = comment.getFoodId();
            QueryWrapper<Foods> foodsQueryWrapper = new QueryWrapper<>();
            foodsQueryWrapper.eq("food_id", foodId);
            Foods foods = foodsMapper.selectOne(foodsQueryWrapper);

            // 注入 foods 对象
            comment.setFoods(foods);

            // 注入头像
            comment.setUserAvatar(userAvatar);
        }

        return comments;
    }

    @Override
    public int insertCommentByCommentRecord(Comments comments) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        simpleDateFormat.applyPattern("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);

        comments.setCommentTime(format);
        return commentsMapper.insert(comments);
    }
}
